package roslan.musicbox.client.data;

import javax.swing.ImageIcon;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import roslan.musicbox.data.AbstractMusicTag;
import roslan.musicbox.exception.TagElementException;
import roslan.musicbox.utils.UIComponentUtils;

public class ClientMusicTag extends AbstractMusicTag {
	public static ImageIcon defaultPic = null;
	public static final int PIC_SIZE = 150;
	public static final String UNKNOWN = "δ֪";
	
	private String tagVersion;
	private ImageIcon apic;
	private boolean isTagUnknown;
	
	public static void setDefaultPic(String filename){
		defaultPic = new ImageIcon(filename);
		defaultPic = UIComponentUtils.scaleImageIcon(defaultPic, PIC_SIZE, PIC_SIZE);
	}
	
	public ClientMusicTag(){
		isTagUnknown = false;
	}
	
	public boolean isTagUnknown(){
		return isTagUnknown;
	}
	
	public void setUnknown(){
		title = UNKNOWN;
		album = UNKNOWN;
		year = UNKNOWN;
		genre = UNKNOWN;
		artist = UNKNOWN;
		comment = UNKNOWN;
		trackNumber = UNKNOWN;
		tagVersion = UNKNOWN;
		isTagUnknown = true;
	}
	
	public ClientMusicTag clone(){
		ClientMusicTag tag = new ClientMusicTag();
		tag.title = this.title;
		tag.album = this.album;
		tag.year = this.year;
		tag.genre = this.genre;
		tag.artist = this.artist;
		tag.comment = this.comment;
		tag.tagVersion = this.tagVersion;
		tag.apic = this.apic;
		return tag;
	}
	
	public String getTagVersion() {
		return tagVersion;
	}
	public void setTagVersion(String tagVersion) {
		this.tagVersion = tagVersion;
	}
	public ImageIcon getApic() {
		return apic;
	}
	public void setApic(ImageIcon apic) {
		this.apic = apic;
	}
	public String toString(){
		return "[Title: "+title+"], "+
			   "[Album: "+album+"], "+
			   "[Year: "+year+"], "+
			   "[Artist: "+artist+"], "+
			   "[Genre: "+genre+"], "+
			   "[TrackNumber: "+trackNumber+"], "+
			   "[Comment: "+comment+"], "+
			   "[TagVersion: "+tagVersion+"]. ";
			   
	}
	
	public static ClientMusicTag getTagFromElement(Element e) 
	throws TagElementException{
		if(!e.getNodeName().equals("MusicTag")){
			throw new TagElementException();
		}
		ClientMusicTag tag = new ClientMusicTag();

		Element eTitle = (Element)e.getElementsByTagName("Title").item(0);
		Text tTitle = (Text)eTitle.getFirstChild();
		if(tTitle == null){
			tag.title = "";
		}else{
			tag.title = tTitle.getTextContent();
		}
		
		Element eAlbum = (Element)e.getElementsByTagName("Album").item(0);
		Text tAlbum = (Text)eAlbum.getFirstChild();
		if(tAlbum == null){
			tag.album = "";
		}else{
			tag.album = tAlbum.getTextContent();
		}
		
		Element eYear = (Element)e.getElementsByTagName("Year").item(0);
		Text tYear = (Text)eYear.getFirstChild();
		if(tYear == null){
			tag.year = "";
		}else{
			tag.year = tYear.getTextContent();
		}
		
		Element eGenre = (Element)e.getElementsByTagName("Genre").item(0);
		Text tGenre = (Text)eGenre.getFirstChild();
		if(tGenre == null){
			tag.genre = "";
		}else{	
			tag.genre = tGenre.getTextContent();
		}
		
		Element eArtist = (Element)e.getElementsByTagName("Artist").item(0);
		Text tArtist = (Text)eArtist.getFirstChild();
		if(tArtist == null){
			tag.artist = "";
		}else{
			tag.artist = tArtist.getTextContent();
		}

		Element eTrackNumber = (Element)e.getElementsByTagName("TrackNumber").item(0);
		Text tTrackNumber = (Text)eTrackNumber.getFirstChild();
		if(tTrackNumber == null){
			tag.trackNumber = "";
		}else{
			tag.trackNumber = tTrackNumber.getTextContent();
		}
		
		Element eComment = (Element)e.getElementsByTagName("Comment").item(0);
		Text tComment = (Text)eComment.getFirstChild();
		if(tComment == null){
			tag.comment = "";
		}else{
			tag.comment = tComment.getTextContent();
		}
		
		return tag;
	}
	public Element getElement(Document doc){
		Element e = doc.createElement("MusicTag");
		
		Element eTitle = doc.createElement("Title");
		Text tTitle = doc.createTextNode(title);
		eTitle.appendChild(tTitle);
		e.appendChild(eTitle);
		
		Element eAlbum = doc.createElement("Album");
		Text tAlbum = doc.createTextNode(album);
		eAlbum.appendChild(tAlbum);
		e.appendChild(eAlbum);
		
		Element eYear = doc.createElement("Year");
		Text tYear = doc.createTextNode(year);
		eYear.appendChild(tYear);
		e.appendChild(eYear);
		
		Element eGenre = doc.createElement("Genre");
		Text tGenre = doc.createTextNode(genre);
		eGenre.appendChild(tGenre);
		e.appendChild(eGenre);
		
		Element eArtist = doc.createElement("Artist");
		Text tArtist = doc.createTextNode(artist);
		eArtist.appendChild(tArtist);
		e.appendChild(eArtist);
		
		Element eComment = doc.createElement("Comment");
		Text tComment = doc.createTextNode(comment);
		eComment.appendChild(tComment);
		e.appendChild(eComment);
		
		Element eTrackNumber = doc.createElement("TrackNumber");
		Text tTrackNumber = doc.createTextNode(trackNumber);
		eTrackNumber.appendChild(tTrackNumber);
		e.appendChild(eTrackNumber);
		
		return e;
	}	
	
}
